package screen;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;

import main.GameCanvas;
import model.Animal;
import model.Command;
import model.IAction;
import model.ItemInfo;
import model.MyObj;
import utilities.GameResource;
import utilities.TField;
import font.BitmapFont;

public class ShopScr extends Screen {

    static ShopScr instance;

    public static ShopScr gI() {
        if (instance == null) {
            instance = new ShopScr();
        }
        return instance;
    }
    public int numTab;
    public int focusTab = 0;
    public int focus = 0;
    public int wCell = 32;
    
    public final int m_col = 4;
    public int m_row;
    public int m_rowDisplay = 4;
    
    public Vector[] m_lstItemInShop;
    public Vector[] m_lstItemInStock;
    public Vector[] m_curLstItem;
    
    private boolean isShowPrice = false;
    public boolean m_bIsKeyArrow = false;
    public boolean isShowStock = false;
    public boolean m_bIsShowPopupInfo = true;
    
    public Screen lastScr;
   
    public int m_numberPrice = 1;
    int m_deltaX = 2;

    Command cmdClear;
    
    public TField tfInput;

    public int m_iFocusX;
    public int m_iFocusY;
    
    //show shop
    public void switchToMe() {
        lastScr = GameCanvas.currentScreen;
        xL = GameCanvas.h + 50;
        m_bIsShowPopupInfo = true;
        super.switchToMe();
    }

    public void setShowPrice(boolean isShow){
    	isShowPrice = isShow;
    	tfInput.setText("1");
    	setCommand();
    }
    
    public boolean isShowPrice(){
    	return isShowPrice;
    }
    
    //close shop
    public void close() {
    	isShowStock = false;
        GameCanvas.cameraList.close();
        lastScr.switchToMe();
    
        System.gc();
    }

    public ShopScr() {
        //init size of shop
        m_width = wCell * (m_col + 1) + 2 * (m_col + 3) + 30;
        m_height = wCell * m_rowDisplay + 2 * (m_rowDisplay + 1) + 60;
        m_left = GameCanvas.hw - m_width / 2;
        m_top = (GameCanvas.h - m_height) / 2;

        m_bIsKeyArrow = true;
        tfInput = new TField();
		tfInput.width = 30;
		tfInput.height = Screen.ITEM_HEIGHT + 2;
		tfInput.setIputType(TField.INPUT_TYPE_NUMERIC);
        tfInput.setMaxTextLenght(2);
        
        //create close command
        /*
        m_cmdRight = new Command(GameResource.close, new IAction() {
            public void perform() {
                if (isShowPrice) {
                	tfInput.isFocus = true;
                    isShowPrice = false;
                } else {
                
                    close();
                }
            }
        });
        */
        
        // add command clear
        cmdClear = new Command(GameResource.close, new IAction() {
            public void perform() {
                close();
            }
        });
        setCommand();
    }

    public void setCommand(){
    	if(!isShowPrice){
    		m_cmdRight = cmdClear;
    		m_cmdLeft = MainScr.gI().stockCmd;
    	}
    	else{
    		m_cmdLeft = new Command(GameResource.close, new IAction() {
                public void perform() {
                	tfInput.isFocus = true;
                    isShowPrice = false;
                    m_cmdRight = cmdClear;
                    m_cmdLeft = MainScr.gI().stockCmd;
                }
            });
    		m_cmdRight = tfInput.cmdClear;
    	}
    }
    
    public MyObj getSelectedItem() {
        if (focus < m_curLstItem[focusTab].size()) {
            return (MyObj) m_curLstItem[focusTab].elementAt(focus);
        } else {
            return null;
        }
    }

    //set items in shop
    public void setShopItems(Vector[] listItemInShop, Command centerCmd) {
        focusTab = 0;
        m_cmdCenter = null;
        m_cmdCenter = centerCmd;
        
        m_lstItemInShop = null;
        m_curLstItem = null;
        m_curLstItem = listItemInShop;

        numTab = m_curLstItem.length;
        isShowPrice = false;
        m_bIsShowPopupInfo = true;
        setCmyLim();
    }
    
    public void setShopItems(Vector[] listItemInShop){
    	focusTab = 0;

        m_lstItemInShop = null;
        m_curLstItem = null;
        m_curLstItem = listItemInShop;

        numTab = m_curLstItem.length;
        isShowPrice = false;
        m_bIsShowPopupInfo = true;
        setCmyLim();
    }

    //set limit for camera and reset focus
    public void setCmyLim() {
        focus = 0;
        m_row = m_curLstItem[focusTab].size() / m_col;
        if (m_curLstItem[focusTab].size() % m_col != 0) {
            m_row = m_curLstItem[focusTab].size() / m_col + 1;
        }

        if (m_row < m_rowDisplay) {
            m_row = m_rowDisplay;
        }

        GameCanvas.cameraList.setInfo(m_left + 5, m_top + 22, wCell, wCell, wCell * m_col, wCell * m_row, wCell * m_col, m_rowDisplay * wCell, 1);
    }

    public void update() {
        lastScr.update();
        if (xL != 0) {
            xL += (-xL >> 1);
        }
        if (xL == -1) {
            xL = 0;
        }
        if(isShowPrice){
        	tfInput.update();
        }
    }

    public void updateKey() {
        if (isShowPrice) {
            updatePrice();
        } else {
            updateKeyMain();
        }
        super.updateKey();
    }
    
    public void keyPress(int keyCode) {
    	if(isShowPrice){
    		
    		
    		m_bIsKeyArrow = false;
    		String str = tfInput.getText();
    		
			if(str.length() == 2 && keyCode >= 0 && keyCode != 10){
				
    			tfInput.clear();
    			tfInput.clear();
    		}
			
    		tfInput.keyPressed(keyCode);
			
    		str = tfInput.getText();
    		
			if(!str.equals("") && str != "0")
				m_numberPrice = Integer.parseInt(str);
			else
				m_numberPrice = 1;
			super.keyPress(keyCode);
    	}
	}

    private void updatePrice() {
        if (GameCanvas.isPointerClick) {

            int y = GameCanvas.h - 140;
            int x = m_left + (m_width >> 1) + 10;

            if (GameCanvas.isPointer(x - 40, y, 40, 10)) {
                setNumberPrice(-1);
                return;
            } else if (GameCanvas.isPointer(x, y, 40, 10)) {
                setNumberPrice(1);
                return;
            }
            return;
        }

        if (GameCanvas.keyPressed[4]) {
            GameCanvas.keyPressed[4] = false;
            setNumberPrice(-1);
        } else if (GameCanvas.keyPressed[6]) {
            GameCanvas.keyPressed[6] = false;
            setNumberPrice(1);
        }
    }

    
    private void setNumberPrice(int dir) {
    	m_bIsKeyArrow = true;
        m_numberPrice += dir;
        if (m_numberPrice < 1) {
            m_numberPrice = 99;
        }
        if (m_numberPrice > 99) {
            m_numberPrice = 1;
        }
        tfInput.setText(""+m_numberPrice);
    }

    public void updateKeyMain() {
        if (GameCanvas.isPointerClick) {
            if (GameCanvas.isPointer(m_left, m_top, m_width, m_height)) {
                if (GameCanvas.isPointer(55 + m_left, 5 + m_top, 70, 25)) {
                    setCmyLim();
                } else {
                    int y = 25;
                    for (int i = 0; i < numTab; i++) {
                        if (GameCanvas.isPointer(5 + m_left, y + m_top, 60, 20)) {
                            setTab(i);
                            return;
                        }
                        y += 20;
                    }
                    for (int i = 0; i < m_row * m_col; i++) {
                        int col = i % m_col;
                        int row = i / m_col;

                        int x = 80 + (wCell + 1) * col - CameraList.cmx;
                        y = m_top + 30 + (wCell + 2) * row - CameraList.cmy;

                        if (GameCanvas.w > 240) {
                            if (GameCanvas.isPointer(x + wCell, y, wCell, wCell)) {
                                if (focus == i) {
                                    GameCanvas.keyPressed[5] = true;
                                } else {
                                    focus = i;
                                }
                                GameCanvas.cameraList.setSelect(focus);
                                return;
                            }
                        } else {
                            if (GameCanvas.isPointer(x, y, wCell, wCell)) {
                                if (focus == i) {
                                    GameCanvas.keyPressed[5] = true;
                                } else {
                                    focus = i;
                                }
                                GameCanvas.cameraList.setSelect(focus);
                                return;
                            }
                        }
                    }
                }
            }
        }

        if (GameCanvas.keyPressed[2]) {
            if (focus / m_col > 0) {
                focus -= m_col;
            } else if (focus / m_col == 0) {
                focus = (m_row - 1) * m_col + focus;
            }
            GameCanvas.cameraList.setSelect(focus);
            GameCanvas.keyPressed[2] = false;
        } else if (GameCanvas.keyPressed[4]) {
            focus--;
            if (focus % m_col == m_col - 1 || focus < 0) {
                setTab(focusTab - 1);
            }
            GameCanvas.keyPressed[4] = false;
        } else if (GameCanvas.keyPressed[6]) {
            focus++;
            if (focus == m_col * m_row) {
                focus = 0;
            }
            GameCanvas.keyPressed[6] = false;
        } else if (GameCanvas.keyPressed[8]) {
            if (focus / m_col + 1 < m_row) {
                focus += m_col;
            } else {
                focus = focus % m_col;
            }
            GameCanvas.cameraList.setSelect(focus);
            GameCanvas.keyPressed[8] = false;
        }
    }
        
    //change tab
    private void setTab(int dir) {
        focusTab = dir;
        if (focusTab == numTab) {
            focusTab = 0;
        }
        if (focusTab < 0) {
            focusTab = numTab - 1;
        }
        setCmyLim();
    }

    
    
    public void paint(Graphics g) {
       
        lastScr.paint(g);
        g.translate(m_left, m_top);
        g.setClip(0, 0, GameCanvas.w, GameCanvas.h);

        PaintPopup.paintRoundRect(g, 0, 0, m_width, m_height - 20, 0x3c8dc0);
        int y = 25;
        for (int i = 0; i < numTab; i++) {
            if (i == focusTab) {
                PaintPopup.paintRoundRect(g, 5, y, 60, 20, 0x9be5ff);
                GameResource.instance.paintShopIcon(g, i, 30, y, 0);
            } else {
                PaintPopup.paintRoundRect(g, 25, y, 50, 20, 0x48c1e6);
                GameResource.instance.paintShopIcon(g, i, 35, y, 0);
            }
            y += 22;
        }

       PaintPopup.paintRoundRect(g, 55, 5, 70, 25, 0x9be5ff);

        //paint name 
       if(isShowStock)
    	   BitmapFont.drawBoldFont(g, GameResource.container, 90, 6, 0x3a001e, Graphics.LEFT | Graphics.HCENTER);
       else
    	   BitmapFont.drawBoldFont(g, GameResource.shop, 90, 6, 0x3a001e, Graphics.LEFT | Graphics.HCENTER);
          
        PaintPopup.paintRoundRect(g, 55, 20, 145, 155, 0x9be5ff);
        g.setColor(0x9be5ff);
        g.fillRect(55, 20, 50, 50);

        if (m_curLstItem[focusTab] != null) {
            g.setClip(0, 25, m_width, 144);
            g.translate(0, -CameraList.cmy);

            //paint all cell
            int x = 0;
            int focusX = 0;
            int focusY = 0;
            int a = m_row * m_col;
            for (int i = 0; i < a; i++) {
                int col = i % m_col;
                int row = i / m_col;

                x = 60 + wCell * col + 2 * (col);
                y = 30 + wCell * row + 2 * (row);
                PaintPopup.paintCell(g, x, y, wCell);

                //paint selected cell
                if (i == focus) {
                    g.setColor(0x22ff77);
                    g.fillRect(x + 1, y + 1, wCell - 2, wCell - 2);

                    focusX = x;
                    focusY = y;
                }

                if (i < m_curLstItem[focusTab].size()) {
                	MyObj obj = (MyObj) m_curLstItem[focusTab].elementAt(i);
                	if(obj != null){
                		obj.paintIcon(g, x, y);
	                	if(obj instanceof Animal){ 
	                		Animal ani = (Animal) obj;
	                		PaintPopup.paintBarBlood(g, x+1, y+29, 30, (ani.animalInfo.m_sTimeLiving + ani.m_iBonusTime)*1440, ani.m_iLifeTime, 0x91c6e1, 0x3a001e);
	                	}
                	}
                }
            }

            g.translate(-g.getTranslateX(), -g.getTranslateY());
            g.setClip(0, 0, GameCanvas.w, GameCanvas.h);

            //paint item info
            if (focus < m_curLstItem[focusTab].size() && m_bIsShowPopupInfo) {
                MyObj obj = (MyObj) m_curLstItem[focusTab].elementAt(focus);
                if(obj != null){
                	m_iFocusX = m_left + focusX;
                	m_iFocusY = m_top + focusY + wCell - CameraList.cmy;
                	obj.paintInfo(g, m_iFocusX, m_iFocusY);
                }
            }

            if (isShowPrice) {
                paintPrice(g);
            }
        }


        super.paint(g);
    }

    
    //paint price for selected item
    public void paintPrice(Graphics g) {
    	
        MyObj obj = (MyObj) m_curLstItem[focusTab].elementAt(focus);
        if (obj instanceof ItemInfo) {
            g.translate(-g.getTranslateX(), -g.getTranslateY());

            int xx = m_left;
            int yy = GameCanvas.h - 140;
            int tmp = xx + (m_width >> 2);
            g.setClip(xx, yy, m_width, 60);
            PaintPopup.paintBox(g, tmp+25, yy, m_width / 3, 60);

            yy += 30;
           // tmp += 20;
            ItemInfo itemInfo = (ItemInfo) obj;

            String str = m_numberPrice + "";
            xx = xx + (m_width >> 1) + 10;
            tmp = BitmapFont.m_facedeFont.stringWidth(str);
            ////////
            tfInput.paintPrice(g, xx-8, yy - 28, m_bIsKeyArrow, str);
            
            PaintPopup.paintArrow(g, 1, xx - 10 - (tmp >> 1), yy - 25, 20 + tmp, m_deltaX);
            m_deltaX = 2 - m_deltaX;

            if (itemInfo.m_iMoney[0] > 0 && focusTab != 6) {
                BitmapFont.drawOutlinedString(g, itemInfo.m_iMoney[0] * m_numberPrice + GameResource.space + GameResource.gold, xx, yy - 10, 0xffff00, 0x000000, Graphics.HCENTER);
            }
            if (itemInfo.m_iMoney[1] > 0 && focusTab != 6) {
                BitmapFont.drawOutlinedString(g, itemInfo.m_iMoney[1] * m_numberPrice + GameResource.space + GameResource.dina, xx, yy + 5, 0xffff00, 0x000000, Graphics.HCENTER);
            }
        }
        
    	
    }
    
    public void paintMarqueeMsg(Graphics g){
	}
}